package com.apexedu.eas.util;

import com.apexedu.framework.jdbc.DBConn;
import oracle.jdbc.OracleTypes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.CallableStatement;
import java.sql.Connection;

/**
 * 调用存储过程
 * Created with IntelliJ IDEA.
 * User: ly
 * Date: 13-10-21
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
public class ProcedureUtil {

	private static final Log log = LogFactory.getLog(ProcedureUtil.class);

	public static ProResult execProcQueryResultSet(String proName, String user, String ip, int opertype, String opername, Object... parms) {
		Connection conn = DBConn.getConnection();
		try {
			CallableStatement ctm = conn.prepareCall("{ call " + proName + "}");
			ctm.registerOutParameter(1, OracleTypes.NUMBER);
			ctm.registerOutParameter(2, OracleTypes.VARCHAR);
			ctm.setString(3, user);
			ctm.setString(4, ip);
			ctm.setInt(5, opertype);
			ctm.setString(6, opername);
			if (parms != null) {
				for (int i = 0; i < parms.length; i++) {
					if (parms[i] != null) {
						ctm.setString(7 + i, parms[i].toString());
					}
				}
			}
			ctm.execute();
			ProResult pr = new ProResult(ctm.getInt(1), ctm.getString(2));
			return pr;
		} catch (Exception e) {
			log.error("execProcQueryResultSet error : " + e);
		}
		return null;
	}

	/**
	 * 存储过程返回对象
	 */
	public static class ProResult {
		private int retval;
		private String retmsg;

		public ProResult() {
		}

		public ProResult(int retval, String retmsg) {
			this.retval = retval;
			this.retmsg = retmsg;
		}

		public int getRetval() {
			return retval;
		}

		public void setRetval(int retval) {
			this.retval = retval;
		}

		public String getRetmsg() {
			return retmsg;
		}

		public void setRetmsg(String retmsg) {
			this.retmsg = retmsg;
		}
	}
}

